import Vue from 'vue'
import Vuex from 'vuex'
import { registerSelf, login, resetPassword, registerCom, forgetPassword, getContract, sign, changeInfo, getSigned, message } from '../api/api'

Vue.use(Vuex)

const state = {
  showPopup: true,
  showFooter: true,
  showBack: true,
  showLoad: false,
  showToast: {
    show: false,
    type: '',
    text: '成功了'
  },
  contracts: [],
  selectFooter: true,
  token: '',
  userType: '',
  background: true
}
const getters = {
  getPopup: state => {
    return state.showPopup
  },
  getFooter: state => {
    return state.showFooter
  },
  getShowBack: state => {
    return state.showBack
  },
  getShowLoad: state => {
    return state.showLoad
  },
  getShowToast: state => {
    return state.showToast
  },
  getSelectFooter: state => {
    return state.selectFooter
  },
  getToken: state => {
    return state.token
  },
  getBackground: state => {
    return state.background
  }
}
const mutations = {
  setPopup (state, boolen) {
    state.showPopup = boolen
  },
  setFooter (state, boolen) {
    state.showFooter = boolen
  },
  setShowBack (state, boolen) {
    state.showBack = boolen
  },
  setShowLoad (state, boolen) {
    state.showLoad = boolen
  },
  setShowToast (state, obj) {
    state.showToast.show = obj.show
    state.showToast.type = obj.type
    state.showToast.text = obj.text
  },
  setSelectFooter (state, boolen) {
    state.selectFooter = boolen
  },
  setToken (state, token) {
    state.token = token
  },
  setContract (state, contract) {
    state.contracts = contract
  },
  setUserType (state, type) {
    state.userType = type
  },
  setBackground (state, type) {
    state.background = type
  }
}
const actions = {
  /**
   * 个人注册注册成功后记录token
   * @param {*} param0
   * @param {obj} 用户输入框信息
   */
  registerSelf ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      registerSelf(userInfo.realname, userInfo.cellphone, userInfo.id_number, userInfo.password, userInfo.code).then(response => {
        let data = response.data
        if (data.Code === 201) {
          if (data.Token) {
            commit('setToken', data.Token)
            commit('setUserType', data.Type)
            localStorage.userType = data.Type
            localStorage.token = data.Token
          }
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 企业注册,注册成功后跳转页面
   * @param {*} param0
   * @param {obj} userInfo 用户输入框信息
   */
  registerCom ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      registerCom(userInfo.cellphone, userInfo.password, userInfo.name, userInfo.organizationCode, userInfo.code, userInfo.types).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 登陆,登陆成功后记录token
   * @param {*} param0
   * @param {obj} userInfo 用户输入框信息
   */
  login ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo.cellphone, userInfo.password).then(response => {
        let data = response.data
        if (data.Code === 300) {
          if (data.Token) {
            commit('setToken', data.Token)
            commit('setUserType', data.Type)
            localStorage.userType = data.Type
            localStorage.token = data.Token
          }
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 修改密码
   * @param {*} param0
   * @param {obj} userInfo 用户输入框信息
   */
  resetPasswrd ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      resetPassword(userInfo.oldPassword, userInfo.newPassword, userInfo.code).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 忘记密码
   * @param {*} param0
   * @param {*} userInfo 用户输入信息
   */
  forgetPassword ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      forgetPassword(userInfo.phone, userInfo.password, userInfo.code).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 获取需签合同
   * @param {*} param0
   */
  getContract ({ commit }) {
    return new Promise((resolve, reject) => {
      getContract().then(response => {
        let result = response.data
        commit('setContract', result.Contract)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 获取已签合同
   * @param {*} param0
   */
  getSigned ({ commit }) {
    return new Promise((resolve, reject) => {
      getSigned().then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
/**
 * 手动签
 * @param {*} param0
 * @param {string} id 合同ID
 */
  sign ({ comit }, id) {
    return new Promise((resolve, reject) => {
      sign(id).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 修改用户手机和邮箱
   * @param {*} param0
   * @param {} userInfo 用户输入信息
   */
  changeInfo ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      changeInfo(userInfo.cellphone, userInfo.email, userInfo.code).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /**
   * 获取短信
   * @param {*} param0
   * @param {*} cellphone 用户输入信息
   */
  message ({ commit }, cellphone) {
    return new Promise((resolve, reject) => {
      message(cellphone).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }
}
const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})

export default store
